﻿using Avalonia.Controls;
using Avalonia.Controls.Templates;

namespace Digitalmes.AvaloniaUI.DataTemplates;

/// <summary>
/// 视图定位器，根据 ViewModel 找到对应的 View。
/// </summary>
public sealed class ViewLocator : IDataTemplate
{
    public Control? Build(object? data)
    {
        if (data is null)
            return null;

        // 若 ViewModel 为单例且有设置 View，直接复用 View。
        if (data.GetType().IsDefined(typeof(VmSingletontLifeTimeAttribute), false)
            && data is IViewToViewModelBridge bridge0
            && bridge0.View != null)
        {
            return (Control)bridge0.View;
        }

        var router = App.Current!.Services.GetRequiredService<IViewRouter>();
        var viewType = router.Routes.FirstOrDefault(s => s.ViewModelType == data.GetType())?.ViewType;
        if (viewType != null)
        {
            try
            {
                var control = (Control)Activator.CreateInstance(viewType)!;
                control.DataContext = data;

                // 桥接器，将 View 注入 ViewModel 中，以便能获取 TopLevel
                if (data is IViewToViewModelBridge bridge)
                {
                    bridge.View = control;
                }

                return control;
            }
            catch (Exception ex)
            {
                return new TextBlock { Text = $"Exception: {ex.Message} " };
            }
        }

        return new TextBlock { Text = $"Not Found: {data.GetType().Name} " };
    }

    public bool Match(object? data)
    {
        return data is ViewModelBase;
    }
}
